<!DOCTYPE html>
<html>
  <head>
    <!--Import Google Icon Font-->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <!--Import materialize.css-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

    <!--Let browser know website is optimized for mobile-->
    <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no' name='viewport' />
     <!--icon-->
     <meta name="title" content="Hospital - Health Ledger">
<link rel="icon" type="image/png" href="../assets/img/icon.png">
<style>
        .dropdown-trigger{
            margin-top : 50px;
        }

        .fixed-nav {
        overflow: hidden;
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 1;
        }

        .padding-top-form {
        padding-top: 5rem;
        }
</style>
<title>
Hospital Dashboard

</title>
</head>

  <body>
      <!-- Navbar start  -->
      <nav class="fixed-nav">
    <div class="nav-wrapper">
      <a href="#" class="brand-logo center">Assign organ</a>
          <ul id="nav-mobile" class="right hide-on-med-and-down">
             <li><a href="reciever.html">Be a Reciever</a></li>
          </ul>
          <ul id="nav-mobile" class="right hide-on-med-and-down">
            <li><a href="donor.html">Donor</a></li>
          </ul>
          <ul id="nav-mobile" class="right hide-on-med-and-down">
            <li><a href="history.html">History</a></li>
          </ul>
          <ul id="nav-mobile" class="right hide-on-med-and-down">
            <li><a href="login.html">Login</a></li>
          </ul>
          <ul id="nav-mobile" class="right hide-on-med-and-down">
            <li><a href="register.html">Register</a></li>
          </ul>
          <ul id="nav-mobile" class="right hide-on-med-and-down">
                <li><a href="../index.html">Home</a></li>
            </ul>
    </div>
  </nav>
  <!-- Navbar end  -->
  <!-- Form start  -->

  <div class="row DonorForm center padding-top-form">
        <!-- <div class="col s6 m6 offset-m3 ">
          <div class="card-panel white">
            <span class="grey-text">
                <div class="row">
                    <h2> Transfer organ  </h2>
                </div>
            </span>
          </div>
        </div> -->
        <div class="row">
            <div class="col m6 offset-m3">
                    <a class='col s12 m12 dropdown-trigger btn' href='#' data-target='hospitalList'>Select Hospital</a>
                    <ul id='hospitalList' class=' dropdown-content'>
                            <!-- <li id="0">hospital1</li> -->
                    </ul>
            </div>
        </div>
        <div class="row">
            <div class="col s12 m12 ">
          <div class="card-panel white">
            <span class="grey-text">
                <div class="row">
                    <div class="col s6 m6">
          <div class="card-panel white">
            <span class="grey-text">
                <div class="row">
      <div class="collection" >
            <a class="collection-header"><h4>Donor List</h4></a>
            <div id="donorList">
            <!-- <a href="#!" class="collection-item">Alvin</a>
            <a href="#!" class="collection-item active">Alvin</a>
            <a href="#!" class="collection-item">Alvin</a>
            <a href="#!" class="collection-item">Alvin</a> -->
        </div>
          </div>
                </div>
            </span>
          </div>
        </div>
        <div class="col s6 m6  ">
                <div class="card-panel white">
                  <span class="grey-text">
                      <div class="row">
                            <div class="collection">
                                <div id="receiverList">
                                    <a class="collection-header"><h4>Reciever List</h4></a>
                                    <!-- <a href="#!" class="collection-item">Alvin</a>
                                    <a href="#!" class="collection-item active">Alvin</a>
                                    <a href="#!" class="collection-item">Alvin</a>
                                    <a href="#!" class="collection-item">Alvin</a> -->
                                  </div>
                      </div>
                  </span>
                </div>
              </div>
             
                </div>
                <div class="row">
                        <div class="input-field col s6 m6 offset-m3">
                            <input  id="incentive" type="text" class="validate">
                            <label for="incentive">Incentive</label>
                        </div>
                </div>
                <div class="row">
                        <div class="col s6 m6 offset-m3">
                            <button id="assign" class="btn waves-effect waves-light" type="submit" name="action">Assign
    <i class="material-icons right">send</i>
  </button>
                        </div>
                </div>

            </span>
          </div>
        </div>
        </div>
      </div>
                
  <!-- Form end  -->

    <!--JavaScript at end of body for optimized loading-->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
    
        <script>
             $('.dropdown-trigger').dropdown();
    </script>
    <script>
     var hospitalListUrl="http://localhost:3000/api/api.Hospital";
    var hospitalList=[];
    $.getJSON(hospitalListUrl,function(data){
        console.log(data);
        $.each(data, function(key,val){
            // console.log($("#hospitalList").append("<li id='"+key+"'>"+val.name +"</li>"))
            hospitalList.push(val);
            $("#hospitalList").append("<li class='list-item' id='"+key+"'>"+val.name +"</li>");
        });
        // console.log(hospitalList);
    });
    var hospitalId;
    $("#hospitalList").on("click","li",function() {
        $(".collection-item").remove();
        hospitalId=$(this).attr('id');
        var selectedHospital=hospitalList[hospitalId];
        /* For Displaying all the donor's */
        var DonorGetUrl="http://localhost:3000/api/api.Donor";
        $.getJSON(DonorGetUrl,function(data){
        // console.log(data[0].firstName);
        var donorList=[];
        $.each(data,function(key,val){
            // console.log(val.preferedHospitals[0].hospitalId);
            // console.log(selectedHospital.hospitalId);
            if(val.preferedHospitals[0].hospitalId==selectedHospital.hospitalId){
                // console.log(val.firstName);
                $("#donorList").append("<a id='donorItem' donorId='"+val.donorId+ "'class='collection-item' id='"+key+"'>"+val.firstName +"(   Blood group: " + val.bloodGroup + "   Organ: " + val.organToDonate.name +")"+"</a>");
            }
        })
    });
    /* For Displaying all the receiver's */
    var ReceiverGetUrl="http://localhost:3000/api/api.Receiver";
        $.getJSON(ReceiverGetUrl,function(data){
            
        // console.log(data[0].firstName);
        var receiverList=[];
        $.each(data,function(key,val){
            // console.log(val.preferedHospitals[0].hospitalId);
            // console.log(selectedHospital.hospitalId);
            if(val.preferedHospitals[0].hospitalId==selectedHospital.hospitalId){
                // console.log(val.firstName);
                $("#receiverList").append("<a id='receiverItem'  receiverId='"+val.receiverId+ "' class='collection-item' id='"+key+"'>"+val.firstName +"(   Blood group: " + val.bloodGroup + "   Organ: " + val.requiredOrgan.name+")"+"</a>");
            }
        })
    });
    });
    /* For selecting the Reciever */
    $("#donorList").on("click","a",function() {
        $(this).addClass("active");
        $(this).addClass("activeDonor")
    });
    $("#receiverList").on("click","a",function() {
        $(this).addClass("active");
        $(this).addClass("activeReceiver");
    });

    // Now do the transaction 
    $("#assign").click(function(){
        event.preventDefault();
        var incentive=$("#incentive").val();
        var fromId=$(".activeDonor").attr("donorId");
        var from="resource:api.Donor#"+fromId;
        var toId=$(".activeReceiver").attr("receiverId");
        var to="resource:api.Receiver#"+toId;
        console.log(from);
        console.log(to);
        console.log(incentive);
        var transactionUrl="http://localhost:3000/api/api.OrganTransfer";
        $.post(transactionUrl,{
    $class: "api.OrganTransfer",
    from: from ,
    to: to,
    rewardPonits: incentive
}).done(function(data){
    console.log("success");
    console.log(data);
    console.log(fromId);
var deleteDonorUrl="http://localhost:3000/api/api.Donor/"+fromId;
console.log(deleteDonorUrl);
$.ajax({
    type: "DELETE",
    url: deleteDonorUrl,
    data: {},
    success: function(msg){
        console.log("Data Deleted: " + msg);
    }
});

console.log(toId);
var deleteReceiverUrl="http://localhost:3000/api/api.Receiver/"+toId;
console.log(deleteReceiverUrl);
$.ajax({
    type: "DELETE",
    url: deleteReceiverUrl,
    data: {},
    success: function(msg){
        console.log("Data Deleted: " + msg);
    }
});
M.toast({html: 'Assigned Organ'})
location.reload(true);
})



    })
    </script>
  </body>
</html>